<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <!-- 进度条未走过的区域，背景颜色是灰色 -->
    <div class="bar-inner">
      <!-- 已走过的进度条 -->
      <div class="progress" ref="progress"></div>
      <div class="progress-btn-wrapper" ref="progressBtn" @touchstart.prevent="progressTouchStart" @touchmove.prevent="progressTouchMove" @touchend="progressTouchEnd">
        <!-- 进度条上的图标 -->
        <div class="progress-btn"></div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { prefixStyle } from '@/assets/js/dom'

// 按钮宽度
const progressBtnWidth = 16
const transform = prefixStyle('transform')

export default {
  props: {
    // 百分比
    percent: {
      type: Number,
      default: 0
    }
  },
  created() {
    this.touch = {}
  },
  methods: {
    progressTouchStart(e) {
      // 已经初始化
      this.touch.initiated = true
      this.touch.startX = e.touches[0].pageX
      // console.log(this.touch.startX)
      this.touch.left = this.$refs.progress.clientWidth
    },
    progressTouchMove(e) {
      if (!this.touch.initiated) {
        return
      }
      // 得到拖动的距离
      const deltaX = e.touches[0].pageX - this.touch.startX
      // 进度条的拖动开始的长度+拖动的长度等于现在的长度
      // 最小为0，最大为进度条总长度减去按钮的长度
      const offsetWidth = Math.min(
        Math.max(0, this.touch.left + deltaX),
        this.$refs.progressBar.clientWidth - progressBtnWidth
      )
      this._offset(offsetWidth)
    },
    progressTouchEnd() {
      this.touch.initiated = false
      this._triggerPercent()
    },
    _offset(offsetWidth) {
      // 已走过的进度条长度变化
      this.$refs.progress.style.width = `${offsetWidth}px`
      // 按钮偏移
      this.$refs.progressBtn.style[
        transform
      ] = `translate3d(${offsetWidth}px,0,0)`
    },
    _triggerPercent() {
      const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
      const percent = this.$refs.progress.clientWidth / barWidth
      this.$emit('percentChange', percent)
    },
    progressClick(e) {
      // console.log(e)
      // 直接获取offset的时候，点击按钮获取的offset会非常小甚至为0
      // this._offset(e.offsetX)
      // 获取按钮最原始的位置离屏幕的距离
      const rect = this.$refs.progressBar.getBoundingClientRect()
      // console.log(rect)
      // e.pagex的意思是现在按钮到屏幕左侧的距离
      const offsetWidth = e.pageX - rect.left
      console.log(e)
      this._offset(offsetWidth)
      this._triggerPercent()
    }
  },
  watch: {
    percent(newPercent) {
      // 百分比大于等于0且不处于拖动的状态
      if (newPercent >= 0 && !this.touch.initiated) {
        // 进度条宽度减去小球宽度等于最多能走过的距离
        const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
        // 偏移宽度等于播放进度百分比乘以最多能走的距离
        const offsetWidth = newPercent * barWidth
        this._offset(offsetWidth)
      }
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~@/assets/stylus/variable';

.progress-bar {
  height: 30px;

  .bar-inner {
    position: relative;
    top: 13px;
    height: 4px;
    background: rgba(0, 0, 0, 0.3);

    .progress {
      position: absolute;
      height: 100%;
      background: $color-theme;
    }

    .progress-btn-wrapper {
      position: absolute;
      left: -8px;
      top: -13px;
      width: 30px;
      height: 30px;

      .progress-btn {
        position: relative;
        top: 7px;
        left: 7px;
        box-sizing: border-box;
        width: 16px;
        height: 16px;
        border: 3px solid $color-text;
        border-radius: 50%;
        background: $color-theme;
      }
    }
  }
}
</style>